package org.adorsys.aderp.jpa.impl;

import org.adorsys.aderp.jpa.api.change.ChangeRecordPublisher;
import org.springframework.beans.factory.BeanFactory;


public class RepositoryResolver {

	@SuppressWarnings("rawtypes")
	private AderpJPARepository repository;
	
	private boolean repositoryResolved;
	
	private RepositoryMap repositoryMap;
	
	private Class<?> jpaKlass;
	
	private ChangeRecordPublisher changeRecordPublisher;

	public RepositoryResolver(BeanFactory beanFactory, Class<?> jpaKlass) {
		this.repositoryMap = beanFactory.getBean(RepositoryMap.class);
		this.jpaKlass = jpaKlass;
		this.changeRecordPublisher = beanFactory.getBean(ChangeRecordPublisher.class);
	}

	@SuppressWarnings("rawtypes")
	protected AderpJPARepository getRepository(){
		if(repository==null && !repositoryResolved) {
			repository = repositoryMap.getRepository(jpaKlass);
			repositoryResolved = true;
		}
		return repository;
	}

	public ChangeRecordPublisher getChangeRecordPublisher() {
		return changeRecordPublisher;
	}
	
}
